package com.ptma.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ptma.model.User;
import com.ptma.repository.UserRepo;
import com.ptma.security.AuthorisedUser;

@Service("userDetailsService")
public class UserDetailsServiceDao implements UserDetailsService {
	@Autowired
	private UserRepo repo;

	@Transactional
	public UserDetails loadUserByUsername(String userId)
			throws UsernameNotFoundException {

		User user = repo.findByEmail(userId.toLowerCase());

		return new AuthorisedUser(user, user.getRoles());
	}

}
